﻿using System;
using System.Drawing;
using System.Windows.Forms;
using MUnit;

namespace Memory
{
    class TestForm
    {
        MemoryForm form = new MemoryForm();

        [Setup]
        void Setup()
        {
            // This will be run once before each test method
        }

        [Test]
        void ClientSize()
        {
            Verify.Equals(form.ClientRectangle.Size.Width / 3, form.ClientRectangle.Size.Height / 2);
        }

        [Test]
        void LoadImages()
        {
            Verify.Equals(MemoryForm.Columns * MemoryForm.Rows / 2 + 1, form.Images.Count);
        }

        [Test]
        void CheckIndexes()
        {
            int size = MemoryForm.Columns * MemoryForm.Rows / 2 + 1;

            int[] result = new int[size];

            foreach (Control control in this.form.Controls)
            {
                TButton button = control as TButton;
                result[button.Index]++;
            }

            Verify.Equals(0, result[0]);
            for (int i = 1; i < size; i++)
            {
                Verify.Equals(2, result[i]);
            }
        }

        [Test]
        void ClickButton()
        {
            TButton button = this.form.Controls[0] as TButton;
            button.TButton_Click(this, null);

            Verify.Assert(button.IsOpen);
        }

        [Test]
        void CloseButton()
        {
            TButton button = this.form.Controls[0] as TButton;
            button.TButton_Click(this, null);
            button.Close();
            Verify.Assert(!button.IsOpen);
        }

        [Test]
        void FindPair()
        {
            TButton first = this.form.Controls[0] as TButton;
            first.TButton_Click(this, null);

            TButton second = null;
            foreach (Control control in this.form.Controls)
            {
                second = control as TButton;
                if (second != first)
                {
                    if (second.Index == first.Index)
                    {
                        second.TButton_Click(this, null);
                        break;
                    }
                }
            }

            Verify.Assert(first.IsOpen);
            Verify.Assert(first.IsLocked);
            Verify.Assert(second.IsOpen);
            Verify.Assert(second.IsLocked);
        }

        [Test]
        void FindNoPair()
        {
            TButton first = this.form.Controls[0] as TButton;
            first.TButton_Click(this, null);

            TButton second = null;
            foreach (Control control in this.form.Controls)
            {
                second = control as TButton;
                if (second != first)
                {
                    if (second.Index != first.Index)
                    {
                        second.TButton_Click(this, null);
                        break;
                    }
                }
            }
            first.TButton_Click(this, null);

            Verify.Assert(first.IsOpen);
            Verify.Assert(!first.IsLocked);
            Verify.Assert(!second.IsOpen);
            Verify.Assert(!second.IsLocked);
        }

        [Test]
        void ClickSameButtonTwice()
        {
            TButton first = this.form.Controls[0] as TButton;
            first.TButton_Click(this, null);
            first.TButton_Click(this, null);

            Verify.Assert(!first.IsLocked);
        }

        [Test]
        void GameFinished()
        {
            foreach (Control control in this.form.Controls)
            {
                TButton second = control as TButton;
                second.Lock();
            }
            Verify.Assert(this.form.IsGameFinished());
        }

        [Test]
        void GameNotFinished()
        {
            Verify.Assert(!this.form.IsGameFinished());
        }

        [TearDown]
        void TearDown()
        {
            // This will be run once after each test method
        }
    }
}
